﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoadTeam.Model;
using System.Data;
using RoadTeam.Enums;
using RoadTeam.SysInterface;
using RoadTeam.Pub;

namespace RoadTeam.BLL
{
    public class CsRoomTypeBll
    {
        private static object o = new object();

        public DataTable GetRoomTypeData(String OrganIdentifier)
        {
            ReflashData(OrganIdentifier);
            SqlWhere where = new SqlWhere();
            where.Info = new CsRoomType() { OrganIdentifier = OrganIdentifier };
           // where.SelectColumn = @" RoomTypeIdentifier,RoomTypeName,RoomTypeCode ";
            return CsRoomType.Select(where, GlobalConfig.ConnectionString);
        }

        /// <summary>
        /// 同步服务器中的房间类型信息
        /// </summary>
        /// <param name="OrganIdentifier">对接系统中的机构标识</param>
        public void ReflashData(String OrganIdentifier)
        {
            String OrganID = FactoryClass.CreateSmOrgan().GetOraganID(OrganIdentifier);

            DataTable dt_Roomtype = GetEmptyTable();
            DataRow dr_Roomtype;
            lock (o)
            {
                DataTable dt = FactoryClass.CreateCsRoomType().GetRoomTypeTable(OrganID);
                foreach (DataRow dr in dt.Rows)
                {
                    dr[2] = CreateIdentifier.GetIdentifier();
                    dr_Roomtype = dt_Roomtype.NewRow();
                    dr_Roomtype["RoomTypeIdentifier"] = dr[2];
                    dr_Roomtype["RoomTypeName"] = dr[0];
                    dr_Roomtype["RoomTypeCode"] = dr[1];
                    dr_Roomtype["OrganIdentifier"] = OrganIdentifier;
                    dt_Roomtype.Rows.Add(dr_Roomtype);
                }
                FactoryClass.CreateCsRoomType().UpdateRoomTypeTable(dt);
            }
            CsRoomType.InsertDatatable(dt_Roomtype, GlobalConfig.ConnectionString);
        }

        public DataTable GetEmptyTable()
        {
            SqlWhere where = new SqlWhere();
            where.SelectColumn = @" RoomTypeIdentifier,RoomTypeName,RoomTypeCode,OrganIdentifier ";
            where.Info = new Model.CsRoomType() { RoomTypeIdentifier = "-1" };
            return CsRoomType.Select(where, GlobalConfig.ConnectionString);
        }

    }
}
